package zstack

import (
	. "bc/util"
	"encoding/json"
	"errors"
)

type Console struct {
	
	
}
/**
	获取云主机控制台地址和访问协议(GetVmConsoleAddress)
 */
func (c *Console) GetConsoleAddresses (uuid string) (string,error){

	oauth ,err := Oauth()
	if err != nil{
		return "",err
	}

	url := Config.Zstack.ApiUrl + "v1/vm-instances/" + uuid + "/console-addresses"

	result,err := Curl.Header(map[string]string{
		"Content-Type":"application/json;charset=UTF-8",
		"Authorization":"OAuth " + oauth,
	}).Url(url).GET()

	if err != nil{
		return "",err
	}
	return result,nil
}

/**
请求控制台访问地址
*/
func (c *Console) GetConsole (uuid string) (map[string]interface{},error){

	oauth,err := Oauth()
	if err != nil{
		return nil,err
	}
	url := Config.Zstack.ApiUrl + "v1/consoles"

	b := make(map[string]interface{},1)
	p := make(map[string]string,1)
	p["vmInstanceUuid"] = uuid
	b["params"] = p

	body,_ := json.Marshal(b)
	h := make(map[string]string)
	h["Content-Type"] = "application/json;charset=UTF-8"
	h["Authorization"] = "OAuth " + oauth

	result,err := Curl.Header(h).Body(string(body)).Url(url).POST()
	inventory := make(map[string]interface{})
	for{
		r := NetWork.GetResults(result)
		if _,ok := r["error"];ok {
			return nil,errors.New("请重试")
		}
		if v,ok := r["inventory"];ok {

			inventory = v.(map[string]interface{})
			break
		}
	}

	return inventory,nil
}